package com.example.springboot.service.impl;

import com.example.springboot.dao.ShopRepository;
import com.example.springboot.pojo.Base;
import com.example.springboot.pojo.Shop;
import com.example.springboot.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ShopServiceImpl  implements ShopService {
    @Autowired
    ShopRepository shopRepository;
    //查询所有商品
    @Override
    public Base findAll(Integer page, Integer size) {
        PageRequest pageRequest = PageRequest.of(page-1, size);
        Page<Shop> shops = shopRepository.findAll(pageRequest);
        return new Base(shops,"ok",1,shops.getTotalElements());
    }
    //新增和更改
    @Override
    public Base save(Shop shop) {
        shopRepository.saveAndFlush(shop);
        if (shop==null){
            return new Base(null,"失败",0,null);
        }
        return new Base(null,"ok",1,null);
    }
    //删除单个商品
    @Override
    public Base del(Shop shop) {
        shopRepository.deleteById(shop.getId());
        return null;
    }
    //查询单个商品详情
    @Override
    public Base findById (int shopId){
        Optional<Shop> byId = shopRepository.findById(shopId);
        if (byId!=null){
            return new Base(byId,"ok",1,null);
        }
        return new Base(byId,"未找到该商品",0,null);
    }
}
